package cn.shop.dys.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.activity.ProductListShowActivity;
import cn.shop.dys.bean.BrandInfo;
import cn.shop.dys.constant.CommonConstant;
import cn.shop.lib_common.CommonUtil;

/**
 * Created by ZhangJun 2016/4/23 16:08
 */
public class RecommBrandApdapter extends RecyclerView.Adapter<RecommBrandApdapter.MyViewHolder> implements View.OnClickListener {
    public RecommBrandApdapter() {
    }

    private Context mContext;
    private List<BrandInfo> mBrandInfos;

    public RecommBrandApdapter(Context context, List<BrandInfo> brandInfos) {
        this.mContext = context;
        this.mBrandInfos = brandInfos;
    }

    @Override
    public RecommBrandApdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 创建viewholder
        MyViewHolder holder = new MyViewHolder(View.inflate(parent.getContext(), R.layout.view_recombrand_rcv_item, null));
        return holder;
    }

    @Override
    public int getItemCount() {
        return mBrandInfos == null ? 0 : mBrandInfos.size();
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        MyViewHolder viewHolder = (MyViewHolder) holder;
        BrandInfo brandInfo = mBrandInfos.get(position);
        String picPortUrl = brandInfo.getPic();
        String picUrl = CommonConstant.HOST + picPortUrl;
        int brandId = brandInfo.getId();
        //BrandInfo{id=1217, name='IMG', pic='/images/brand/f.png'}
        Log.d("推荐品牌adapter", picUrl);
        Glide.with(mContext).load(picUrl).into(viewHolder.iv);
        viewHolder.iv.setTag(brandId);
        viewHolder.iv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        CommonUtil.showSafeToast(mContext,"去商品列表");
        Intent intent = new Intent(mContext, ProductListShowActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("key","brand/plist");
        bundle.putString("id",""+(int) v.getTag()); //品牌id..
        intent.putExtras(bundle);
        mContext.startActivity(intent);

    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        public ImageView iv;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv_recombrand_item);
        }
    }

    private OnRecyclerViewItemClickListener mOnRecyclerViewItemClickListener;

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, BrandInfo clickItemData);
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.mOnRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }
}

